const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const WxParse = require('../Common/wxParse/wxParse.js');
Page({
  data: {
    id:'',
    detaildata:'',
    content:'',
    kfconcatnums:'',
    iswechat:false,
    isshare:false,
    fullpath:'',
    scene:'',
  },
  onLoad: function (option) {
    if(option.id){
      this.setData({
        id:option.id
      })
      this.goodsDetail()
    }
    if(option.parentid){
      app.globalData.parentid=option.parentid
      app.globalData.companyid=option.companyid
    }
    // if(option.scene){
    //   var scene=JSON.parse(decodeURIComponent(option.scene))
    //   this.setData({
    //     id:scene.id
    //   })
    // }
    if(option.scene){
      var attr=decodeURIComponent(option.scene).split('&');
      this.setData({
        id:attr[0].split('id=')[1]
      })
      this.goodsDetail()
    }
    if(!wx.getStorageSync('logintoken')){
      app.ToLogin(false)
    }
    // if(option.parentid){
    //   var scene=JSON.parse(decodeURIComponent(option.scene))
    //   app.globalData.parentid=scene.parentid
    //   app.globalData.companyid=scene.companyid
    // }
    if(option.scene){
      var attr=decodeURIComponent(option.scene).split('&');
      if(attr[1]){
        app.globalData.parentid=attr[1].split('parentid=')[1];
      }
      if(attr[2]){
        app.globalData.companyid=attr[2].split('companyid=')[1];
      }
    }
  },
  golink(e){
    wx.navigateTo({
      url:e.currentTarget.dataset.url
    });
  },
  toLocation(e){
    var lng=e.currentTarget.dataset.lng;
    var lat=e.currentTarget.dataset.lat;
    var name=e.currentTarget.dataset.name;
    var address=e.currentTarget.dataset.address;
    wx.openLocation({
    //当前经纬度
      longitude: Number(lng),
      latitude: Number(lat),
      //缩放级别默认28
      scale: 28,
      //位置名
      name: name,
      //详细地址
      address: address,
      //成功打印信息
      success: function(res) {
          console.log(res)
      },
      //失败打印信息
      fail: function(err) {
          console.log(err)
      },
      //完成打印信息
      complete: function(info) {
          console.log(info)
      },
    })
  },
  onShow(){
    
  },
  share(){
    if(!wx.getStorageSync('logintoken')){
      app.ToLogin(false)
      return
    }
    if(wx.getStorageSync('logintoken')){
      let userInfo=wx.getStorageSync('userInfo') || '';
      let path="pages/detail/detail";
      // let path="/pages/detail/detail?id="+this.data.id+"&parentid="+userInfo.id+"&companyid="+this.data.detaildata.companyId;
      let scene={
        id:this.data.id,
        parentid:userInfo.id,
        companyid:this.data.detaildata.companyId
      }
      this.setData({
        fullpath:path,
        scene
      })
    }
    this.setData({
      isshare:true,
      
    })
    
  },
  goodsDetail(){
    Controller.jobdetail({jobId:this.data.id}).then(res=>{
      if(res.data.baseData){
        res.data.baseData.jobPersonPhone=res.data.baseData.jobPersonPhone.split("，");
        this.setData({
          detaildata:res.data.baseData
        })
        if(res.data.baseData.company && res.data.baseData.company.desc){
          // res.data.baseData.content = JSON.stringify(res.data.baseData.company.desc).replace(/<img/gi, "<img class='richImg' style='width:auto!important;height:auto!important;max-height:100%;width:100%;'");
          // res.data.baseData.content = JSON.parse(res.data.baseData.company.desc);
          var content=res.data.baseData.company.desc
          var that = this;
          WxParse.wxParse('content', 'html', content, that, 5);
          
        }
      }
    })
  },
  concat(e){
    var tel=e.currentTarget.dataset.tel;
    wx.showActionSheet({
      itemList: ['呼叫'],
      success:function(res){
        if(res.tapIndex==0){
          wx.makePhoneCall({
            phoneNumber:tel,
          })
        }
      }
    })
  },
  kefu(){
    this.setData({
      iswechat:true
    })
  },
  cancel(){
    this.setData({
      iswechat:false
    })
  },

  buy(e){
    if(!wx.getStorageSync('logintoken')){
      app.goLogin(false)
      return
    }
    var that=this;
    wx.showModal({
      title: '',
      content: '是否投递个人简历',
      confirmColor:'#F08519',
      success (res) {
        if (res.confirm) {
          var data={
            companyId:that.data.detaildata.companyId,
            jobId:that.data.id
          }
          console.log(app.globalData)
          if(app.globalData.companyid==that.data.detaildata.companyId){
            data.parentUserId=app.globalData.parentid
          }
          Controller.delivery(data).then(res=>{
            if(res.code==0){
              wx.showToast({
                title:'投递成功'
              })
            }else{
              wx.showToast({
                title:res.msg,
                icon:"none"
              })
              if(res.code==-9){
                setTimeout(res=>{
                  wx.navigateTo({
                    url:"/pages/Me/editresume"
                  });
                },1500)
              }
            }
            
          }).catch(res=>{
            
            wx.showToast({
              title:res.msg,
              icon:"none"
            })
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  onUnload(){
    wx.switchTab({
      url:"/pages/Index/Index"
    })
  },
  previewImage(e){

    var urls=[];
    urls=urls.concat(e.currentTarget.dataset.url)


    wx.previewImage({
      current: e.currentTarget.dataset.url, // 当前显示图片的http链接
      urls:urls, // 需要预览的图片http链接列表
    })
  },
  
  onShareAppMessage: function (e) {
    let title = '';
    let imageUrl = '';
    let userInfo=wx.getStorageSync('userInfo') || '';
    let path="/pages/detail/detail?id="+this.data.id+"&parentid="+userInfo.id+"&companyid="+this.data.detaildata.companyId
    console.log(path)
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {
        console.log('点击页面内的分享按钮--onShareAppMessage分享成功', res)
      },
      fail(err) {
        console.log('点击页面内的分享按钮--onShareAppMessage-fail', err)
      }
    }
    
    return obj
  },
  onShareTimeline: function(res){
    let title = '';
    let imageUrl = '';
    let userInfo=wx.getStorageSync('userInfo') || '';
    let path="/pages/detail/detail?id="+this.data.id+"&parentid="+userInfo.id+"&companyid="+this.data.detaildata.companyId
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {
        console.log('点击页面内的分享按钮--onShareAppMessage分享成功', res)
      },
      fail(err) {
        console.log('点击页面内的分享按钮--onShareAppMessage-fail', err)
      }
    }
    
    return obj
  },
 
})